<?php

namespace app\modules\admin\models;

use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\BaseActiveRecord;

class Qrcode extends ActiveRecord
{
    public $qrcode_img;

    public static function tableName()
    {
        return 'wx_qrcode';
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'relation' => '关联资源',
            'description' => '资源描述',
            'type' => '二维码类型', // 1 - 临时的整型 2 - 临时的字符串 3 - 永久的整型 4 - 永久的字符串
            'scene_id' => '场景值ID',
            'scene_str' => '场景值字符串',
            'expire_seconds' => '有效时间',
            'ticket' => '二维码凭证',
            'url' => '解析地址',
            'qrcode_img' => '二维码图片',
            'created_at' => '创建时间',
        ];
    }

    public function rules()
    {
        return [
            [['type'], 'required'],
            [['scene_id', 'scene_str'], 'unique'],
            ['scene_id', 'integer', 'max' => 100000],
            ['scene_str', 'string', 'length' => [1, 64]],
            ['expire_seconds', 'integer', 'max' => 2592000],
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'attributes' => [
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
                'value' => time(),
            ],
        ];
    }

    // 获取最新一个ID
    public function getLastId()
    {
        $id = Qrcode::find()->orderBy(['id' => SORT_DESC])->scalar();
        return empty($id) ? 0 : $id;
    }
}
